﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using Gem_Tools;
using Gem_Tools_Entity.Gem_Tools;
using Gem_Tools_Controller.Gem_Tools;
using Gem_Tools_Utility;

namespace GE_Management_Tool.Planning
{
    public partial class frmA0018_Add_Tasks : frmBase
    {
        IList<CStaff> m_lsStaff;
        int m_iStaff_ID;
        IList<CAsset> m_lsAsset;
        IList<CTasks> m_lsTask;
        Boolean is_New = true;
        CAppointments m_Appointment;

        public CAppointments Show_To_Display(int p_iStaff_ID)
        {
            m_lsTask = new List<CTasks>();
            cTasksBindingSource.DataSource = m_lsTask;
            m_iStaff_ID = p_iStaff_ID;

            this.ShowDialog();
            return m_Appointment;
        }

        public CAppointments Show_To_Edit(CAppointments p_Appo)
        {
            is_New = false;
            m_Appointment = p_Appo;

            m_lsTask = new List<CTasks>();
            CTasks_Controller v_CtrlTask = new CTasks_Controller();
            m_lsTask = v_CtrlTask.A001_sp_sel_List_Tasks_By_Project_ID_And_Asset_ID(CConfig.g_intGem_Tools_Current_Project, m_Appointment.Auto_ID);
            cTasksBindingSource.DataSource = m_lsTask;

            this.ShowDialog();
            return m_Appointment;
        }

        public frmA0018_Add_Tasks()
        {
            InitializeComponent();
            //CCommon_Function.Format_All(this);
            grvData.OptionsBehavior.Editable = true;
            dtmStart.Properties.DisplayFormat.FormatString = "dd-MMM-yyyy HH:mm";
            dtmEnd.Properties.DisplayFormat.FormatString = "dd-MMM-yyyy HH:mm";
            dtmStart.Properties.EditFormat.FormatString = "dd-MMM-yyyy HH:mm";
            dtmEnd.Properties.EditFormat.FormatString = "dd-MMM-yyyy HH:mm";
        }

        private void Init_Data()
        {
            m_lsStaff = new List<CStaff>();
            CStaff_Controller v_CtrlStaff = new CStaff_Controller();
            m_lsStaff = v_CtrlStaff.A001_sp_sel_List_Staff_By_Regency_And_Project_ID(CConfig.g_intGem_Tools_Current_Project);

            lkeStaff.Properties.DataSource = m_lsStaff;

            m_lsAsset = new List<CAsset>();
            CAsset_Controller v_CtrlAsset = new CAsset_Controller();
            m_lsAsset = v_CtrlAsset.A001_List_Asset_By_Project_ID(CConfig.g_intGem_Tools_Current_Project);

            lkeProject_Asset.DataSource = m_lsAsset;

            lkeStaff.EditValue = m_iStaff_ID;

            if (is_New == false)
            {
                Assign_Obj_To_Control();
            }
        }

        private void Assign_Obj_To_Control()
        {
            txtTask_Name.Text = m_Appointment.Subject;
            dtmStart.DateTime = m_Appointment.Start_Date;
            dtmEnd.DateTime = m_Appointment.End_Date;
            lkeStaff.EditValue = m_Appointment.Resource_ID;
            tbcAsset.Value = m_Appointment.Percent_Complete / 10;
            txtPriority.Value = m_Appointment.Priority;

        }

        private void Assign_Control_To_Obj()
        {
            m_Appointment.Subject = txtTask_Name.Text;
            m_Appointment.Start_Date = dtmStart.DateTime;
            m_Appointment.End_Date = dtmEnd.DateTime;
            m_Appointment.Resource_ID = CUtility.Convert_To_Int32(lkeStaff.EditValue);
            m_Appointment.Percent_Complete = tbcAsset.Value * 10;
            m_Appointment.Priority = CUtility.Convert_To_Int32(txtPriority.EditValue);
            m_Appointment.Label = m_Appointment.Priority;
            m_Appointment.Project_ID = CConfig.g_intGem_Tools_Current_Project;
        }

        private void frmA0018_Add_Tasks_Load(object sender, EventArgs e)
        {
            try
            {
                if (is_New == true)
                {
                    dtmStart.DateTime = new DateTime(DateTime.Now.Year, DateTime.Now.Month, DateTime.Now.Day, 9, 0, 0);
                    dtmEnd.DateTime = new DateTime(DateTime.Now.Year, DateTime.Now.Month, DateTime.Now.Day, 18, 0, 0);
                }
                
                Init_Data();
            }
            catch (Exception ex)
            {
                XtraMessageBox.Show(ex.Message, "Warning", MessageBoxButtons.OK);
            }
        }

        private string Check_Before_Save()
        {
            double v_dbTotal_Manday = 0;
            StringBuilder v_sb = new StringBuilder();
            foreach (CTasks v_obj in m_lsTask)
            {
                CAsset v_asset = (CAsset)lkeProject_Asset.GetDataSourceRowByKeyValue(v_obj.Asset_ID);
                v_dbTotal_Manday += v_asset.Manday;
            }

            TimeSpan v_ts = new TimeSpan();
            v_ts = dtmStart.DateTime.Subtract(dtmEnd.DateTime);

            //if (v_dbTotal_Manday > v_ts.Days)
            //{
            //    v_sb.AppendLine("Planning Time Cannot greater than total Asset Manday");
            //}

            IList<CAppointments> v_lsCurrent_Start_Task = new List<CAppointments>();
            CAppointments_Controller v_CtrlAppointments = new CAppointments_Controller();
            v_lsCurrent_Start_Task = v_CtrlAppointments.A1004_sp_sel_List_Appointments_Contains_Date(m_iStaff_ID, dtmStart.DateTime, dtmEnd.DateTime);

            foreach (CAppointments v_obj in v_lsCurrent_Start_Task)
            {
                v_sb.AppendLine("Assign Dupicate: " + v_obj.Subject + ";");
            }

            return v_sb.ToString();
        }

        private void btnOK_Click(object sender, EventArgs e)
        {
            try
            {
                string error = Check_Before_Save();
                if (error == "")
                {
                    if (is_New == true)
                    {
                        m_Appointment = new CAppointments();
                        Assign_Control_To_Obj();
                        CAppointments_Controller v_CtrlAppo = new CAppointments_Controller();
                        int p_iTasks_id = v_CtrlAppo.Insert_Appointments(m_Appointment);
                        m_Appointment.Auto_ID = p_iTasks_id;
                        CTasks_Controller v_CtrlTask = new CTasks_Controller();
                        foreach (CTasks v_obj in m_lsTask)
                        {
                            v_obj.Task_ID = p_iTasks_id;
                            v_obj.Project_ID = CConfig.g_intGem_Tools_Current_Project;
                            v_CtrlTask.Insert_Tasks(v_obj);
                        }
                    }
                    else
                    {
                        Assign_Control_To_Obj();
                        CAppointments_Controller v_CtrlAppo = new CAppointments_Controller();
                        v_CtrlAppo.Update_Appointments(m_Appointment);
                        CTasks_Controller v_CtrlTask = new CTasks_Controller();
                        v_CtrlTask.A0018_sp_del_Tasks_By_Tasks_ID(m_Appointment.Auto_ID, CSession.Active_User_Name);
                        foreach (CTasks v_obj in m_lsTask)
                        {
                            v_obj.Task_ID = m_Appointment.Auto_ID;
                            v_obj.Project_ID = CConfig.g_intGem_Tools_Current_Project;
                            v_CtrlTask.Insert_Tasks(v_obj);
                        }
                    }

                    XtraMessageBox.Show("Success !", "Warning", MessageBoxButtons.OK);
                    this.Close();
                }
                else
                {
                    throw new Exception(error);
                }
            }
            catch (Exception ex)
            {
                XtraMessageBox.Show(ex.Message, "Warning", MessageBoxButtons.OK);
            }
        }
    }
}